package com.lgx.right.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.lgx.common.basic.dao.GeneralDao;

import com.lgx.common.exception.DataException;
import com.lgx.right.dao.IResourceDao;
import com.lgx.right.entity.Resource;

/**
 * 
 * @author acer
 *
 */
@Component("resourceDao")
public class ResourceDao extends GeneralDao implements IResourceDao {

	protected Logger log = Logger.getLogger(this.getClass());
	
	@Transactional
	public Resource createResource(Resource resource) throws DataException {
		this.getHibernateTemplate().save(resource);
		log.info("[创建资源]:"+resource.getId());
		return resource;
	}

	@Transactional
	public Resource updateResource(Resource resource) throws DataException {
		this.getHibernateTemplate().update(resource);
		log.info("[更新资源]:"+resource.getId());
		return resource;
	}

	@Transactional
	public void deleteResource(Resource resource) throws DataException {
		log.info("[删除资源]:"+resource.getId());
		this.getHibernateTemplate().delete(resource);
	}

	public void deleteResource(long id) throws DataException {
		this.deleteResource(this.findResourceById(id));
		log.info("[删除资源]:"+id);
	}

	public Resource findResourceById(long id) {
		return this.getHibernateTemplate().get(Resource.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<Resource> findResourceList(String sql) {
		return this.getHibernateTemplate().find(sql);
	}

}
